

/*===================================================
根据表单设定组装语句并执行


===================================================*/

import MySqlCenter from "./MySqlCenter";

class ColumnObj{
    public name:string  = "";     
    public type:COL_TYPE  = COL_TYPE.CT_INT;            //
    public bAddIdx:boolean  = false;    //是否增加索引

}
enum COL_TYPE{
    CT_INT,
    CT_TIME,
    CT_STR64,
    CT_STR256,
}

export default class MysqlAuto{
    public m_szTable:string = "table";  //表名
    public m_vCols:ColumnObj[]  = [];   //表的项目

    private s_szTypes:string[]  = ["int","datetime","varchar(64)","varchar(256)"];
    public constructor(tbname:string){
        this.m_szTable  = tbname;
    }

    //
    public AddColumn(name:string,type:COL_TYPE){
        var pCO = new ColumnObj();
        pCO.name    = name;
        pCO.type    = type;
        this.m_vCols.push(pCO);
    }

    private ctrateTable(){
        
    }

    public InitTable(){//检测数据表是否存在, 不存在就新建
        var pDB = MySqlCenter.getInstance().GetLink();
        var sql = "select count(*) from "+this.m_szTable;
        console.log("InitTable--"+this.m_szTable);
        pDB.query(sql, (error, results, fields)=>{
            //表不存在就创建
            if(error){
                if(error.code == 'ER_NO_SUCH_TABLE'){
                    console.log("try create Table"+this.m_szTable);
                    this.ctrateTable();
                }
                else{
                    console.log('[SELECT ERROR] - ',error.message);
                }
                
                return;
              }
        });
            //             if (error) throw error;
            //             console.log(results[0].val);
            //             //console.log(fields);
            //         });

        // $sql = "CREATE TABLE " . $gameid . "uin (idx int PRIMARY KEY AUTO_INCREMENT, acc varchar(128),logintime datetime,createtime datetime,val int, exstr varchar(256));";
        // $con->query($sql);
        // $sql = "ALTER TABLE " . $gameid . "uin ADD INDEX index_idx ( `idx` ) ";
        // $con->query($sql);
        // $sql = "ALTER TABLE " . $gameid . "uin ADD INDEX index_acc ( `acc` ) ";
        // $con->query($sql);
        // $sql = "ALTER TABLE " . $gameid . "uin auto_increment=10000";
        // $con->query($sql);
    }

    public SelectItems(){

    }

}